package com.myday.system.controller;

import com.myday.system.service.IMyTodoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.icepear.echarts.Bar;
import org.icepear.echarts.Line;
import org.icepear.echarts.Pie;
import org.icepear.echarts.charts.bar.BarSeries;
import org.icepear.echarts.charts.line.LineAreaStyle;
import org.icepear.echarts.charts.line.LineSeries;
import org.icepear.echarts.charts.pie.PieDataItem;
import org.icepear.echarts.charts.pie.PieSeries;
import org.icepear.echarts.components.coord.cartesian.CategoryAxis;
import org.icepear.echarts.components.dataset.Dataset;
import org.icepear.echarts.components.legend.Legend;
import org.icepear.echarts.components.title.Title;
import org.icepear.echarts.render.Engine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: xiaofengs
 * @description : 统计用户的待办事项数据
 * @date: 2024/5/19 15:51
 * @version: 1.0
 */

@RestController
@RequestMapping("/user/statistic")
public class StatisticController extends BaseController {

    @Autowired
    private IMyTodoService myTodoService;

    // 周统计待办事项数据
    @RequestMapping("/category/week")
    @PreAuthorize("@ss.hasPermi('system:todo:list')")
    public AjaxResult week() {
        List<Long> weekStatistic = myTodoService.getWeekStatistic();
        Line lineChart = new Line()
                .addXAxis(new CategoryAxis()
                        .setData(new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"})
                        .setBoundaryGap(false))
                .addYAxis()
                .addSeries(new LineSeries()
                        .setData(new Number[]{
                                weekStatistic.get(0),
                                weekStatistic.get(1),
                                weekStatistic.get(2),
                                weekStatistic.get(3),
                                weekStatistic.get(4),
                                weekStatistic.get(5),
                                weekStatistic.get(6)
                        })
                        .setAreaStyle(new LineAreaStyle()));
        Engine engine = new Engine();
        // It is recommended that you can  get the serialized version of Option in the representation of JSON, which can be used directly in the template or in the RESTful APIs.
        String jsonStr = engine.renderJsonOption(lineChart);
        return success((Object) jsonStr);
    }

    // 月统计待办事项数据
    @RequestMapping("/bar/month")
    @PreAuthorize("@ss.hasPermi('system:todo:list')")

    public AjaxResult month() {
        List<Long> monthStatistic = myTodoService.getMonthStatistic();
        Bar bar = new Bar()
                .setLegend()
                .setTooltip("item")
                .addXAxis(new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"})
                .addYAxis()
                .addSeries(new BarSeries()
                        .setData(new Number[]{
                                monthStatistic.get(0),
                                monthStatistic.get(1),
                                monthStatistic.get(2),
                                monthStatistic.get(3),
                                monthStatistic.get(4),
                                monthStatistic.get(5),
                                monthStatistic.get(6),
                                monthStatistic.get(7),
                                monthStatistic.get(8),
                                monthStatistic.get(9),
                                monthStatistic.get(10),
                                monthStatistic.get(11)
                        }));
        Engine engine = new Engine();
        String jsonStr = engine.renderJsonOption(bar);
        return success((Object) jsonStr);
    }

    // 年统计待办事项数据
    @RequestMapping("/bar/year")
    @PreAuthorize("@ss.hasPermi('system:todo:list')")
    public AjaxResult year() {
        List<Long> yearStatistic = myTodoService.getYearStatistic();
        Bar bar = new Bar()
                .setLegend()
                .setTooltip("item")
                .addXAxis(new String[]{"前年","去年","今年"})
                .addYAxis()
                .addSeries(new BarSeries()
                        .setData(new Number[]{
                                yearStatistic.get(0),
                                yearStatistic.get(1),
                                yearStatistic.get(2)
                        }));
        Engine engine = new Engine();
        String jsonStr = engine.renderJsonOption(bar);
        return success((Object) jsonStr);
    }


    @PreAuthorize("@ss.hasPermi('system:todo:list')")
    @RequestMapping("/round/status")
    public AjaxResult round() {
        // 返回圆环图数据
        Map<String, Long> statusStatistic = myTodoService.getStatusStatistic();
        List<PieDataItem> list = new ArrayList<>();
        statusStatistic.forEach((k, v) -> {
            list.add(new PieDataItem().setValue(v).setName(k));
        });
        PieSeries pieSeries = new PieSeries()
                .setData(list)
                .setType("pie");
        Pie pie = new Pie()
                .setTitle(new Title().setText("待办事项状态分布"))
                .setLegend(new Legend().setOrient("vertical").setLeft("left"))
                .setTooltip("item")
                .addSeries(pieSeries);
        Engine engine = new Engine();
        String jsonStr = engine.renderJsonOption(pie);
        return success((Object) jsonStr);
    }

}
